#include<stdio.h>
#include<string.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include<stdlib.h>

int main()
{
    int fd = open("test.txt",O_RDONLY);
    if(fd < 0) exit(1);
    dup2(fd,0);// 从文件中读

    while(1)
    {
        char buffer[64];
        int n = read(0,buffer,sizeof(buffer));
        if(n) printf("%s",buffer);
        else break;
    }

    return 0;
}

//int main()
//{
//    int fd = open("test.txt",O_CREAT | O_WRONLY | O_APPEND,0666);
//    if(fd < 0) exit(1);
//    dup2(fd,1);
//
//    printf("fd : %d\n",fd);
//    printf("Hello stdout\n");
//    return 0;
//}
//
////int main()
////{
////    umask(0);
////    close(1);
////    // int fd = open("test.txt",O_CREAT | O_WRONLY | O_TRUNC,0666);// 重定向
////    int fd = open("test.txt",O_CREAT | O_WRONLY | O_APPEND,0666);// 追加重定向
////    printf("fd : %d\n",fd);
////    printf("Hello stdout\n");
////    // close(fd);
////    return 0;
////}
////
////
////
//////int main()
//////{
//////
//////    printf("stdin:%d\n",stdin->_fileno);
//////    printf("stdout:%d\n",stdout->_fileno);
//////    printf("stderr:%d\n",stderr->_fileno);
//////
//////    printf("\n\n");
//////    int fd1 = open("log1.txt",O_CREAT | O_WRONLY | O_TRUNC,0666);
//////    int fd2 = open("log2.txt",O_CREAT | O_WRONLY | O_TRUNC,0666);
//////    int fd3 = open("log3.txt",O_CREAT | O_WRONLY | O_TRUNC,0666);
//////    int fd4 = open("log4.txt",O_CREAT | O_WRONLY | O_TRUNC,0666);
//////    int fd5 = open("log5.txt",O_CREAT | O_WRONLY | O_TRUNC,0666);
//////    int fd6 = open("log6.txt",O_CREAT | O_WRONLY | O_TRUNC,0666);
//////
//////    printf("fd1: %d\n",fd1);
//////    printf("fd2: %d\n",fd2);
//////    printf("fd3: %d\n",fd3);
//////    printf("fd4: %d\n",fd4);
//////    printf("fd5: %d\n",fd5);
//////    printf("fd6: %d\n",fd6);
//////
//////    return 0;
//////}
//////
//////
////////int main()
////////{
////////    umask(0);
////////    int fd = open("test.txt",O_RDONLY);
////////   // int fd = open("test.txt",O_CREAT | O_WRONLY | O_TRUNC,0666); // 文件不存在就创建
////////    if(fd < 0)
////////    {
////////        perror("open");
////////        return 1;
////////    }
////////
////////    while(1)
////////    {
////////        char buffer[128];
////////        int n = read(fd,buffer,sizeof(buffer) - 1);
////////        buffer[n] = 0;
////////        if(n > 0)
////////        {
////////            printf("%s",buffer);
////////        }
////////        else break;
////////    }
////////
////////
////////   // const char* msg = "abcd";
////////   // int a = 1234556;
////////   // char buffer[16];
////////   // snprintf(buffer,sizeof(buffer),"%d",a);
////////   // write(fd,buffer,strlen(buffer));
////////    // write(fd,msg,strlen(msg));
////////
//////////    int cnt = 5;
//////////    const char* msg = "hello write\n";
//////////    while(cnt--)
//////////    {
//////////        write(fd,msg,strlen(msg));// 向文件中写的时候不需要给\0留空间
//////////    }
////////
////////    close(fd);
////////    return 0;
////////}
////////
//////////#define ONE_FLAG (1<<0)
//////////#define TWO_FLAG (1<<1)
//////////#define THREE_FLAG (1<<2)
//////////#define FOUR_FLAG (1<<3)
//////////
//////////void Print(int flag)
//////////{
//////////    if(flag & ONE_FLAG)
//////////    {
//////////        printf("One\n");
//////////    }
//////////    if(flag & TWO_FLAG)
//////////    {
//////////        printf("Two\n");
//////////    }
//////////    if(flag & THREE_FLAG)
//////////    {
//////////        printf("Three\n");
//////////    }
//////////    if(flag & FOUR_FLAG)
//////////    {
//////////        printf("Four\n");
//////////    }
//////////}
//////////
//////////int main()
//////////{
//////////    Print(ONE_FLAG);
//////////    printf("\n");
//////////    Print(ONE_FLAG | TWO_FLAG);
//////////    printf("\n");
//////////    Print(ONE_FLAG | TWO_FLAG | THREE_FLAG);
//////////    printf("\n");
//////////    Print(ONE_FLAG | TWO_FLAG | THREE_FLAG | FOUR_FLAG);
//////////    printf("\n");
//////////    Print(TWO_FLAG | FOUR_FLAG);
//////////    printf("\n");
//////////}
//////////
////////////int main()
////////////{
////////////    printf("hello printf\n");
////////////
////////////    fprintf(stdout,"hello fprintf\n");
////////////
////////////    char* msg =(char*)"hello fwrite\n";
////////////    fwrite(msg,strlen(msg),1,stdout);
////////////
////////////    return 0;
////////////}
////////////
//////////////int main(int argc,char* argv[])
//////////////{
//////////////    if(argc != 2)
//////////////    {
//////////////        printf("cat filename\n");
//////////////        return 1;
//////////////    }
//////////////    FILE* fp = fopen(argv[1],"r");
//////////////    if(fp == NULL) 
//////////////    {
//////////////        perror("fopen");
//////////////        return 2;
//////////////    }
//////////////    // size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream)
//////////////    // 读文件
//////////////    while(1)
//////////////    {
//////////////        char buffer[128];
//////////////        memset(buffer,0,sizeof(buffer));
//////////////        int n = fread(buffer,1, sizeof(buffer) - 1,fp);
//////////////        if(n > 0)
//////////////        {
//////////////            printf("%s",buffer);
//////////////			fflush(stdout);
//////////////        }
//////////////        if(feof(fp)) 
//////////////            break;
//////////////    }
//////////////    fclose(fp);
//////////////    return 0;
//////////////}
////////////////
////////////////int main()
////////////////{
////////////////    FILE* fp = fopen("log.txt","w");
////////////////    if(fp == NULL)
////////////////    {
////////////////        perror("fopen");
////////////////        return 1;
////////////////    }
////////////////    int cnt = 1;
////////////////    const char* s = "Hello file";
////////////////    while(cnt <= 10)
////////////////    {
////////////////        char buffer[1024];
////////////////        snprintf(buffer,sizeof(buffer),"%s: %d\n",s,cnt++);
////////////////        fwrite(buffer,strlen(buffer),1,fp);
////////////////    }
////////////////
////////////////    fclose(fp);
////////////////    return 0;
////////////////}
